package Vista;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import Controlador.Sistema;
import DAO.DAOCliente;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class frmClientes extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnAlta;
	private JTable tblClientes;
	private JScrollPane jScrollPane1;
	private JButton frmRefrescar;
	private JButton btnEliminar;
	private JButton btnModificar;
	private Sistema elSistema = Sistema.Getinstancia();
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frmClientes inst = new frmClientes();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public frmClientes() {
		super();
		initGUI();
		

		
		llenarGrilla();
	}
	
	private void llenarGrilla(){
		// Lleno la grilla de clientes

		ArrayList<DAOCliente> clientes = elSistema.ConsultarClientes();
		
		DefaultTableModel tblClientesModel = new DefaultTableModel();
		
		tblClientesModel.addColumn("dni");
		tblClientesModel.addColumn("apellido");
		tblClientesModel.addColumn("nombre");
		
		for (DAOCliente daoCliente : clientes) {
			
			Vector<String> valores = new Vector<String>();
			
			valores.add(String.valueOf(daoCliente.getDni()));
			valores.add(daoCliente.getApellido());
			valores.add(daoCliente.getNombre());
			
			tblClientesModel.addRow(valores);
		}

		tblClientes.setModel(tblClientesModel);
		
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Administracion de Clientes");
			{
				btnAlta = new JButton();
				getContentPane().add(btnAlta);
				btnAlta.setText("Alta");
				btnAlta.setBounds(12, 12, 80, 23);
				btnAlta.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnAltaActionPerformed(evt);
					}
				});
			}
			{
				btnModificar = new JButton();
				getContentPane().add(btnModificar);
				btnModificar.setText("Modificar");
				btnModificar.setBounds(111, 12, 109, 23);
				btnModificar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnModificarActionPerformed(evt);
					}
				});
			}
			{
				btnEliminar = new JButton();
				getContentPane().add(btnEliminar);
				btnEliminar.setText("Eliminar");
				btnEliminar.setBounds(230, 12, 111, 23);
				btnEliminar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnEliminarActionPerformed(evt);
					}
				});
			}
			{
				frmRefrescar = new JButton();
				getContentPane().add(frmRefrescar);
				frmRefrescar.setText("Refrescar (Cambiar por Observer)");
				frmRefrescar.setBounds(361, 12, 233, 23);
				frmRefrescar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						frmRefrescarActionPerformed(evt);
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(12, 46, 589, 340);
				{
					TableModel tblClientesModel = 
						new DefaultTableModel(
								new String[][] { { "One", "Two" }, { "Three", "Four" } },
								new String[] { "Column 1", "Column 2" });
					tblClientes = new JTable();
					jScrollPane1.setViewportView(tblClientes);
					tblClientes.setBounds(373, 57, 231, 162);
					tblClientes.setDragEnabled(true);
					tblClientes.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					tblClientes.setModel(tblClientesModel);
					tblClientes.setPreferredSize(new java.awt.Dimension(586, 312));
				}
			}
			pack();
			this.setSize(629, 436);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void btnModificarActionPerformed(ActionEvent evt) {
		Integer dni = 0;
		
		JTable tabla = this.tblClientes;
        TableModel tablaModelo;
        tablaModelo = (TableModel) this.tblClientes.getModel();
		
	    int registro = tabla.getSelectedRow();
	    if (registro >= 0) {
			dni= Integer.valueOf((String) tablaModelo.getValueAt(registro, 0));
			frmCliente frm = new frmCliente("Modificacion", dni);
			frm.setVisible(true);
	    }
	}
	
	private void frmRefrescarActionPerformed(ActionEvent evt) {
		
		this.llenarGrilla();
		
	}
	
	private void btnEliminarActionPerformed(ActionEvent evt) {
		Integer dni = 0;
		
		JTable tabla = this.tblClientes;
        TableModel tablaModelo;
        tablaModelo = (TableModel) this.tblClientes.getModel();
		
	    int registro = tabla.getSelectedRow();
	    if (registro >= 0) {
			dni= Integer.valueOf((String) tablaModelo.getValueAt(registro, 0));
			frmCliente frm = new frmCliente("Eliminacion", dni);
			frm.setVisible(true);
	    }
	}
	
	private void btnAltaActionPerformed(ActionEvent evt) {
		frmCliente frm = new frmCliente("Alta", -1);
		frm.setVisible(true);
		}

}
